package com.atguigu.lease.web.app.service.impl;


import com.atguigu.lease.common.jwt.JwtUtil;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private AttrValueService attrValueService;
    @Autowired
    private FacilityInfoService facilityInfoService;
    @Autowired
    private LabelInfoService labelInfoService;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private FeeValueService feeValueService;
    @Autowired
    private LeaseTermService leaseTermService;
    @Autowired
    private BrowsingHistoryService browsingHistoryService;



    @Override
    public void customPage(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        roomInfoMapper.customSelectPage(page, queryVo);
    }

    @Override
    public RoomDetailVo customGetById(Long id,String accessToken) {
        //查询roomInfo
        RoomDetailVo roomDetailVo = roomInfoMapper.customSelectById(id);
        //查询所属公寓信息
        ApartmentItemVo apartmentItemVo = apartmentInfoService.customByApartmentId(roomDetailVo.getApartmentId());
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        //房间图片
        List<GraphVo> graphVo = graphInfoService.listByItemTypeAndId(id,ItemType.ROOM);
        roomDetailVo.setGraphVoList(graphVo);
        //属性值
        List<AttrValueVo> attrValueVos = attrValueService.listByRoomId(id);
        roomDetailVo.setAttrValueVoList(attrValueVos);
        //配套信息
        List<FacilityInfo> facilityInfos = facilityInfoService.listByRoomId(id);
        roomDetailVo.setFacilityInfoList(facilityInfos);
        //标签信息
        List<LabelInfo> labelInfos = labelInfoService.listByRoomId(id);
        roomDetailVo.setLabelInfoList(labelInfos);
        //支付方式
        List<PaymentType> paymentTypeList = paymentTypeService.listByRoomId(id);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        //杂费列表
        List<FeeValueVo> feeValueVos = feeValueService.listByApartmentId(roomDetailVo.getApartmentId());
        roomDetailVo.setFeeValueVoList(feeValueVos);
        //租期列表
        List<LeaseTerm> leaseTerms = leaseTermService.listByRoomId(id);
        roomDetailVo.setLeaseTermList(leaseTerms);

        Claims claims = JwtUtil.parseToken(accessToken);
        Integer userId = (Integer) claims.get("userId");
        BrowsingHistory browsingHistory = new BrowsingHistory();
        browsingHistory.setBrowseTime(new Date());
        browsingHistory.setRoomId(id);
        browsingHistory.setUserId(Long.valueOf(userId));
        browsingHistoryService.save(browsingHistory);
        return roomDetailVo;
    }

    @Override
    public void customPageById(Page<RoomItemVo> page, Long id) {
        roomInfoMapper.customSelectPageById(page, id);
    }
}




